How to crete log4dxl library?

Hi, I have to write dxl code and I do not see any custom logging functionality I searched and found something and make little adaptation. I am wondering if there are some leaks and memory problems with it because in one function 100 lines I use it between 10-20 times.

int logLevel = 3
int logFlushEveryNLines = 1000

int lineCounter = 0
Stream logStream = null
bool standardOutput = true

// prints the message to log file according to the logLevel setting
// You have to define logFlushEveryNLines paramter when use this lib.
void printMessage (int level, string text) {
    if (level <= logLevel) {
        string prefix
        if (level == 0) {
            prefix = "ERROR: "
        } else
        if (level == 1) {
            prefix = " WARNING: "
        } else
        if (level == 2) {
            prefix = "  LOG: "
        } else {
            prefix = "   DEBUG: "
        }
        if (!standardOutput) {
            logStream << prefix text
        } else {
            print prefix text
        }
    }
    if (!standardOutput && logFlushEveryNLines > 0) {
        if (lineCounter++ > logFlushEveryNLines) {
            logFlushEveryNLines = 0;
            flush (logStream)
        }
    }
}

void closeLog(){
    if (!standardOutput) {
        flush (logStream)
        close (logStream)
    }
}


Dinio - Tue Jan 05 05:46:29 EST 2016

Re: How to crete log4dxl library?
Mathias Mamsch - Tue Jan 05 06:28:45 EST 2016

No large memory problems here. If you log so much, that you run out of memory you should definitely check your logging ;-)

Anyway your usage of lineCounter is weird. I would expect it to be increased and set to 0 once it hits logFlushEveryNLines. Instead you are setting logFlushEveryNLines to 0?

And you could maybe print prefix and the message separately to avoid concatenating to a new string each time. Regards, Mathias